import org.codehaus.groovy.grails.commons.ConfigurationHolder

class Guser {

    String name
    String password
    String email
    Gprofile profile
    static hasMany = [roles:Grole]
    def roleList = []
    Date activationDate
    String activationCode
    Boolean loggedIn = false

    def List getRoleList() {
        println "starting roles = " + roles
        def defaultRoles = ConfigurationHolder.config.gk.user.defaultRoles
        println "starting defaultRoles = " + defaultRoles
        def tempRoles = []
        println "starting tempRoles = " + tempRoles
        defaultRoles.each {  it->
                tempRoles.add(it)
        }
        println "processed tempRoles = " + tempRoles
        roles.each{ r->
            //tempRoles.add(r.toString())
            if(!tempRoles.contains(r.toString())) {
                print 'r=' + r
                println "before ->" + tempRoles
                tempRoles.add(r.toString())
                println "after ->" + tempRoles
            }
        }
        println "final tempRoles = " + tempRoles
        roleList = tempRoles
        roleList
    }

    def isLoggedIn() {
            loggedIn
    }

	/**
	 * does the contain this role?
	 * might be possible with a roles.contain(roleName)
	 *
 	 * @return boolean
	 */
	def hasRole(roleName) { 

		def hasRole = false
		roles.each { r->
			if(r.name==roleName) { 
				hasRole = true
			}
		}

		hasRole

	}

	def hasPermission(permToCheck) { 
		def hasPerm = false
		roles.each { r->
			def grole = Grole.findByName(r.toString())
			grole.permissions.each { p->
				if (p.toString()==permToCheck.toString()) {
					hasPerm = true
				}
			}
		}	
		return hasPerm
	}
	
    static constraints = {
//        activationCode(blank:true)
//        dateCreated(blank:true)
//        dateActivated(blank:true)
	activationDate:[blank:true,nullable:true]
    }
}
